﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CoolCode.Web.UI.Controls
{
    public partial class Pager : System.Web.UI.UserControl,IPager
    {
        public event GridViewPageEventHandler PageIndexChanging;

        public int PageIndex { get { return (int)(ViewState["PageIndex"] ?? 0); } set { ViewState["PageIndex"] = value; } }
        public int PageSize { get { return (int)(ViewState["PageSize"] ?? 10); } set { ViewState["PageSize"] = value; } }
        public int PageCount { get { return (int)(ViewState["PageCount"] ?? 1); } set { ViewState["PageCount"] = value; } }
        //public int TotalRecords { get { return (int)(ViewState["TotalRecords"] ?? 0); } set { ViewState["TotalRecords"] = value; } }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public virtual void Bind(IPaginatedList p)
        {
            this.PageIndex = p.PageIndex;
            this.PageCount = p.PageCount;
            this.PageSize = p.PageSize;
            //this.TotalRecords = p.TotalRecords;
            if (p.TotalRecords  == 0)
            {
                this.Visible = false;
                return;
            }
            else
            {
                this.Visible = true;
            }
            ltrPageNumber.Text = (this.PageIndex + 1).ToString();
            txbPageNumber.Text = ltrPageNumber.Text;
            ltrPageCount.Text = this.PageCount.ToString();
            btnFirst.Enabled = (this.PageIndex != 0);
            btnPrev.Enabled = (this.PageIndex != 0);
            btnNext.Enabled = (this.PageIndex < this.PageCount - 1);
            btnLast.Enabled = (this.PageIndex < this.PageCount - 1);
            //btnFirst.ImageUrl = btnFirst.Enabled ? "~/App_Themes/" + this.Page.Theme + "/Images/firstpg.gif" : "~/App_Themes/" + this.Page.Theme + "/Images/firstpg_d.gif";
            //btnPrev.ImageUrl = btnPrev.Enabled ? "~/App_Themes/" + this.Page.Theme + "/Images/prevpg.gif" : "~/App_Themes/" + this.Page.Theme + "/Images/prevpg_d.gif";
            //btnNext.ImageUrl = btnNext.Enabled ? "~/App_Themes/" + this.Page.Theme + "/Images/nextpg.gif" : "~/App_Themes/" + this.Page.Theme + "/Images/nextpg_d.gif";
            //btnLast.ImageUrl = btnLast.Enabled ? "~/App_Themes/" + this.Page.Theme + "/Images/lastpg.gif" : "~/App_Themes/" + this.Page.Theme + "/Images/lastpg_d.gif";
            //btnGo.ImageUrl = "~/App_Themes/" + this.Page.Theme + "/Images/go.gif";

            //btnFirst.CssClass = btnFirst.Enabled ? "FirstPage" : "FirstPageDisable";
            //btnPrev.CssClass = btnFirst.Enabled ? "PrevPage" : "PrevPageDisable";
            //btnNext.CssClass = btnNext.Enabled ? "NextPage" : "NextPageDisable";
            //btnLast.CssClass = btnLast.Enabled ? "LastPage" : "LastPageDisable";
            lblDspTotalRecords.Text = p.TotalRecords.ToString();
        }

        protected void PagerButton_Click(object sender, EventArgs e)
        {
            string arg = ((IButtonControl)sender).CommandArgument;
            switch (arg)
            {
                case "first":
                    this.PageIndex = 0;
                    break;
                case "prev":
                    this.PageIndex--;
                    break;
                case "next":
                    this.PageIndex++;
                    break;
                case "last":
                    this.PageIndex = this.PageCount - 1;
                    break;
                case "go":
                    int intNewPageIndex;
                    int.TryParse(txbPageNumber.Text, out intNewPageIndex);
                    intNewPageIndex--;
                    if (intNewPageIndex >= 0 && intNewPageIndex < this.PageCount)
                    {
                        this.PageIndex = intNewPageIndex;
                    }
                    break;
            }
            PageIndexChanging(this, new GridViewPageEventArgs(this.PageIndex));
        }
    }
}